window.onload = function() {
    var oSelect = document.getElementById("allCheck");
    var aItems = document.getElementsByClassName("check");
    var oSum = document.getElementById("allPrice");
    var oGoods = document.getElementById("allNum");
 
    // 全选
    oSelect.onclick = function() {
        if (oSelect.checked) {
            for (var i = 0; i < aItems.length; i++) {
                if (aItems[i].checked) {
 
                } else {
                    oSelect.checked = true;
                    aItems[i].checked = true;
                    getStart();
                }
            }
        } else {
        //全消
            for (var i = 0; i < aItems.length; i++) {
                if (aItems[i].checked) {
                    aItems[i].checked = false;
                    oSum.innerText = 0;
                    oGoods.innerText = 0;
                } else {}
            }
        }
    }
}
 
function getStart() {
    var oSelect = document.getElementById("allCheck");
    var aItems = document.getElementsByClassName("check");
    var oSum = document.getElementById("allPrice");
    var oGoods = document.getElementById("allNum");
 
     var aCount = document.getElementsByClassName("count");
    var aNum = document.getElementsByClassName("num");
    var a = 0;
 
    for (var i = 0; i < aItems.length; i++) {
        if (aItems[i].checked) {
            chooseIt(i);
            a++;
            if (a == 3) {
                oSelect.checked = true; //当所有选项都选到时,全选按钮自动勾选
            }
        } else {
            oSelect.checked = false; //任意一个选项没勾选,全选按钮不选
            chooseIt(i);
        }
    }
}
 
function getSum() {
    var temp = 0;
    var oSum = document.getElementById("allPrice");
    var aItems = document.getElementsByClassName("check");
    var aCount = document.getElementsByClassName("count");
 
    //循环,计算选中的商品的总价格
    for (var j = 0; j < aItems.length; j++) {
        if (aItems[j].checked) {
            temp += parseInt(aCount[j].innerText);
        } else {
            temp += 0;
        }
    }
 
    oSum.innerText = temp;
}
 
function getGoods() {
    var num = 0;
    var oGoods = document.getElementById("allNum");
    var aItems = document.getElementsByClassName("check");
    var aNum = document.getElementsByClassName("num");
 
    //循环,计算选中的商品的总数量
    for (var i = 0; i < aItems.length; i++) {
        if (aItems[i].checked) {
            num += parseInt(aNum[i].value);
        } else {
            num += 0;
        }
    }
 
    oGoods.innerText = num;
}
 
function getTotal(n) {
    var aPrice = document.getElementsByClassName("price");
    var aNum = document.getElementsByClassName("num");
    var aCount = document.getElementsByClassName("count");
 
    //计算单件商品的总价
    var oMoney = parseInt(aPrice[n].innerText) * parseInt(aNum[n].value);
    aCount[n].innerText = oMoney;
}
 
function getPlus(n) {
    var aNum = document.getElementsByClassName("num");
 
    //增加单件商品的数量
    var temp = parseInt(aNum[n].value) + 1;
    aNum[n].value = temp;
}
 
function getReduce(n) {
    var aNum = document.getElementsByClassName("num");
 
    //减少单件商品的数量
    var temp = parseInt(aNum[n].value) - 1;
    aNum[n].value = temp;
 
    //当剪到数量为1件时,停止减
    if (aNum[n].value < 1) {
        aNum[n].value = 1;
    }
}
 
function showTotal() {
    var money = document.getElementById("allPrice").innerText;
    alert("你总共花了" + money + "money");
}
 
function chooseIt(i) {
    var aAdd = document.getElementsByClassName("add");
    var aReduce = document.getElementsByClassName("reduce")
    var aRemove = document.getElementsByClassName("remove");
    var oTab = document.getElementById("cartTable");
 
    aAdd[i].onclick = function() {
        getPlus(i); //增加单件商品数量
        getTotal(i); //计算单间商品总价
        getSum(); //计算总商品价格
        getGoods(); //计算总商品数量
    }
 
    aReduce[i].onclick = function() {
        getReduce(i); //减少单件商品数量
        getTotal(i);
        getSum();
        getGoods();
    }
 
    aRemove[i].onclick = function() {
        var oDialog = confirm("确定要删除吗?");
        if (oDialog) {
            oTab.tBodies[0].removeChild(aRemove[i].parentNode.parentNode);
            getGoods();
            getSum();
        }
        getStart(); //更新表格中行的数量
    }
    getGoods(); //显示初始总商品数量(在勾选了该商品,但未增加数量时,显示默认数量1)
    getSum(); //显示初始商品总价(在勾选了该商品,但未增加数量时,显示默认总价为1件商品的金额)
}